<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
 <%@ page import="java.util.*" %>
 <%@ page import="org.json.simple.JSONObject" %>
 <%@ page import="org.json.simple.parser.JSONParser" %>
 <%@ page import="org.json.simple.JSONArray" %>
 <%@ page import="com.netcracker.studportal.server.facade.*" %> 
 <%@ page import="org.apache.commons.lang.StringEscapeUtils" 
 		  import="java.net.*"
 %>
   
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Student Portal - Student View</title>
<link href="style/css/cssShedule.css" rel="stylesheet" type="text/css">
</head>
<body>

<div class="main">
	<div class="wrapper">
	
	
	<div id="panel-navigation">
			<div class="m">
			<ul id="navigationMenu">
				<li>
					<a class="home" href="index.jsp">
						<span>На главную</span>
					</a>
				</li>
				<li>
					<a class="about" href="<% out.print("logout.jsp"); %>">
						<span>Выйти</span>
					</a>
				</li>
				<li>
					<a class="contact" href="#">
						<span>Контакты</span>
					</a>
				</li>
			</ul>
			<div class="clear"></div>
			</div>
		</div>
	
		<div class="oval11">
			 <a href = "<% out.print("student.jsp?action=schedule"); %>" >
			<div class="content">Расписание экзаменов</div>
			</a>
		</div>
        
        <div class="oval21">
       	   <a href = "<% out.print("student.jsp?action=exam_res"); %>" >
   	    <div class="content">Результаты экзаменов</div>
   	    </a>
        </div>
        
		<div class="oval31">
       	  <a href="<% out.print("student.jsp?action=materials"); %>">
       	  <div class="content">Дополнительные материалы</div>
       	  </a>
        </div>
        
        <div class="cont">
        </div>
		
		<div class="cont-body"> 
		   <div class="general-content">
<%
	if ((session.getAttribute("username") == null) || ("").equals(session.getAttribute("username")))
	{	
		response.sendRedirect("login.jsp");
	}
	
	String student_id = session.getAttribute("user_id").toString();
 	String action = request.getParameter("action");
 	
 	if ("material".equals(action))
	{
		String id = request.getParameter("id");
		FacadeStudent fa = new FacadeStudent();
		JSONObject obj=new JSONObject();
		obj.put("exam_id",id);

		String param = obj.toString();
		param = fa.getMaterialsByExam(param);
	
		JSONParser parser=new JSONParser(); 
		Object ob=parser.parse(param);
		if (ob instanceof JSONArray) {
			JSONArray array=(JSONArray)ob;
			Iterator iter=array.iterator();
			out.print("<ul class='materials'>");
			
			while(iter.hasNext()){
				JSONObject data = (JSONObject) iter.next();
		    	String file_name = URLDecoder.decode(data.get("material_name").toString(), "UTF-8");
		    	String materialId = data.get("material_id").toString();

		    	String link2Material = "download.jsp?material_id=" + materialId + "&user_type=1";
				out.print("<li><a href='" + link2Material + "'>"+file_name+"</a></li>");
			}
			out.print("</ul>");
		} else {
			JSONObject jsonObj = (JSONObject) ob;
			String error = jsonObj.get("error").toString();
			String stacktrace = jsonObj.get("stacktrace").toString();
			
			out.print("Has occurred exception:\n" + error + "\n" + stacktrace);
		}
	}
 	
 	if ("materials".equals(action))
	{
		
		FacadeStudent fa = new FacadeStudent();
		JSONObject obj=new JSONObject();
		obj.put("student_id",student_id);

		String param = obj.toString();
		param = fa.getExamsByStudent(param);
	
		JSONParser parser=new JSONParser(); 
		Object ob=parser.parse(param);
		JSONArray array=(JSONArray)ob;
		
		Iterator iter=array.iterator();
		
		while(iter.hasNext()){
		JSONObject data = (JSONObject) iter.next();
	    String exam = data.get("exam").toString();
	    String exam_id = data.get("exam_id").toString();
	     
		Object ob1=parser.parse(exam);
	    JSONObject data1 = (JSONObject)ob1;
		
	    String s_name = data1.get("subject_name").toString();
	 //   String exam_id = data1.get("exam_id").toString();
	    String p_first_name = data1.get("prof_first_name").toString();
	    String p_patronymic = data1.get("patronymic").toString();
	    String p_last_name = data1.get("prof_last_name").toString();
		
		out.print("<a href='student.jsp?action=material&id="+exam_id+"'><div class='stick'>");
		
		out.print("<p class='title'>Предмет:</p>");
		out.print("<p class='txt'>"+s_name+"</p>");
		out.print("<p class='title'>Преподаватель:</p>");
		out.print("<p class='txt'>"+p_last_name+" "+p_first_name+" "+p_patronymic+"</p>");
		
		out.print("</div></a>");
		}
	}
 	
 	if ("exam_res".equals(action))
	{
		FacadeStudent fa = new FacadeStudent();
		JSONObject obj=new JSONObject();
		obj.put("student_id",student_id);

		String param = obj.toString();
		param = fa.getExamsByStudent(param);
	
		JSONParser parser=new JSONParser(); 
		Object ob=parser.parse(param);
		
		if (ob instanceof JSONArray)
		{
		JSONArray array=(JSONArray)ob;
		
		Iterator iter=array.iterator();
		
		while(iter.hasNext()){
		JSONObject data = (JSONObject) iter.next();
	    String exam = data.get("exam").toString();
	    String mark = (data.get("mark") != null) ? data.get("mark").toString() : "";
	     
		Object ob1=parser.parse(exam);
	    JSONObject data1 = (JSONObject)ob1;
		
	    String s_name = data1.get("subject_name").toString();
	    String p_first_name = data1.get("prof_first_name").toString();
	    String p_patronymic = data1.get("patronymic").toString();
	    String p_last_name = data1.get("prof_last_name").toString();
		
		out.print("<div class='stick'>");
		
		out.print("<p class='title'>Предмет:</p>");
		out.print("<p class='txt'>"+s_name+"</p>");
		out.print("<p class='title'>Преподаватель:</p>");
		out.print("<p class='txt'>"+p_last_name+" "+p_first_name+" "+p_patronymic+"</p>");
		out.print("<p class='title'>Оценка:</p>");
		out.print("<p class='txt'>"+mark+"</p>");
		
		out.print("</div>");
		}
		}
		else
		{
			JSONObject jsonObj = (JSONObject) ob;
  			String error = jsonObj.get("error").toString();
  			out.print("<div>Упс, ошибочка вышла: " + error+"</div>");	
		}
	}
 
 
	if ("schedule".equals(action))
	{
		FacadeStudent fa = new FacadeStudent();
		JSONObject obj=new JSONObject();
		obj.put("student_id",student_id);

		String param = obj.toString();
		param = fa.getExamsByStudent(param);
	
		JSONParser parser=new JSONParser(); 
		Object ob=parser.parse(param);
		if (ob instanceof JSONArray)
		{
		JSONArray array=(JSONArray)ob;
		
		Iterator iter=array.iterator();
		
		while(iter.hasNext())
		{
			JSONObject data = (JSONObject) iter.next();
		    String exam = data.get("exam").toString();
		     
			Object ob1=parser.parse(exam);
		    JSONObject data1 = (JSONObject)ob1;
		    
		    String s_name = data1.get("subject_name").toString();
		    String e_date = data1.get("exam_date").toString();
		    String p_first_name = data1.get("prof_first_name").toString();
		    String p_patronymic = data1.get("patronymic").toString();
		    String p_last_name = data1.get("prof_last_name").toString();
		    
		    out.print("<div class='stick'>");
			
			out.print("<p class='title'>Предмет:</p>");
			out.print("<p class='txt'>"+s_name+"</p>");
			out.print("<p class='title'>Дата:</p>");
			out.print("<p class='txt'>" +  e_date+"</p>");
			out.print("<p class='title'>Преподаватель:</p>");
			out.print("<p class='txt'>"+p_last_name+" "+p_first_name+" "+p_patronymic+"</p>");
			
			out.print("</div>");
		}
		}
		else
		{
			JSONObject jsonObj = (JSONObject) ob;
  			String error = jsonObj.get("error").toString();
  			out.print("<div>Упс, ошибочка вышла: " + error+"</div>");	
		}
	}	
 %>
				</div>
			</div>
        </div>
  </div>

  

</body>
</html>